package com.B1线程优先级;

import java.util.Random;

/**
 * @Auther: SJZ    2907374717@qq.com
 * @Date: 2019/3/12 22:22
 * @Description:优先级具有规则性
 * regularity:规则性
 * 高优先级的线程总是大部分先执行完，但不代表高优先级的线程全部先执行完。
 */
public class PriorityRegularityTest {
    static class MyThread1 extends Thread{
        public void run(){
            long beginTime=System.currentTimeMillis();
            long addResult=0;
            for(int j=0;j<10;j++){
                for(int i=0;i<50000;i++){
                    Random random= new Random();
                    random.nextInt();
                    addResult =addResult+1;
                }
            }

            long endTime=System.currentTimeMillis();
            System.out.println("******** thread1 i use time="+(endTime-beginTime));


        }
    }

    static class MyThread2 extends Thread{
        public void run(){
            long beginTime=System.currentTimeMillis();
            long addResult=0;
            for(int j=0;j<10;j++){
                for(int i=0;i<50000;i++){
                    Random random= new Random();
                    random.nextInt();
                    addResult=addResult+1;
                }
            }
            long endTime= System.currentTimeMillis();
            System.out.println("#########thread 2 use time"+(endTime-beginTime));
        }
    }

    public static void main(String[] args) {
        for(int i=0;i<5;i++){
            MyThread1 myThread1= new MyThread1();
            myThread1.setPriority(10);
            myThread1.start();
            MyThread2 myThread2= new MyThread2();
            myThread2.setPriority(1);
            myThread2.start();;
        }
    }
}
